package com.da.javatest.leetCode;

import java.util.Arrays;
import java.util.List;

/**
 * @author chenlida
 * @date 2019/1/12 15:18
 * @description
 */
public class Code202 {
    public boolean isHappy(int n) {
        List<Integer> list = Arrays.asList(4, 16, 37, 58, 89, 145, 42, 20);
        n = getNum(n);
        if (n == 1) {
            return true;
        } else if (list.contains(n)) {
            return false;
        } else {
            return isHappy(n);
        }
    }

    private int getNum(int n) {
        int total = 0;
        while (n != 0) {
            int r = n % 10;
            total += r * r;
            n /= 10;
        }
        return total;
    }
}
